﻿var savegeometryurl = IServerUrl + "/data-WuLuMuQiKuangZhengGongZuoKongJianFaBu/rest/data/datasources/name/KCK/datasets/name/CKQ_KQFW/";
var m_project_id = '';
var m_xkzh = '';
var m_obj = null;

{
    var r = new RegExp("(^|(.*?\\/))(spcommon.js)(\\?|$)"),
	s = document.getElementsByTagName('script'),
	src, m, baseurl = "";
    for (var i = 0, len = s.length; i < len; i++) {
        src = s[i].getAttribute('src');
        if (src) {
            var m = src.match(r);
            if (m) {
                baseurl = m[1];
                break;
            }
        }
    }
    inputScript(baseurl + 'SaveGeometry.js');
}
function inputScript(inc) {
    var script = '<' + 'script type="text/javascript" src="' + inc + '"' + '><' + '/script>';
    document.writeln(script);
}


var metaconsts = new SmConstClsList();
metaconsts.Init("KCK_JJLX,KZ@GetKZMeta");
$("document").ready(function () {
    m_project_id = $.query.get("Project_ID");
    if (m_project_id != null && m_project_id != '') {
        var dbalias = "CKQDJK";
        var tablename = "采矿申请登记";
        var condittion = "PROJECT_ID=\\\'" + m_project_id + "\\\'";
        m_obj = new SmObjectData("CKQDJK", "采矿申请登记");
        m_obj.GetFirstSmObject(condittion);
        InitSQB();
    }
    var BLHJ = $.query.get("BLHJ");
    if (BLHJ == "PH") {
        $("#btnSQPH").removeClass("nosee");
        $("#btnJSPH").removeClass("nosee");
        $("#btnPrintSPB").removeClass("nosee");
        if ($("#txtZHENGHAO").val() != "") {
        }
    } else if (BLHJ == "ZZ") {
        $("#btnPrintXKZH").removeClass("nosee");
        $("#btnPrintXKZ").removeClass("nosee");
        $("#btnBLWC").removeClass("nosee");
    } else {
        $("#btnPrintXKZH").removeClass("nosee");
        $("#btnPrintXKZ").removeClass("nosee");
        if (m_obj.GetValue("XMLX") == "20") {
            $("#btnBLWC").removeClass("nosee");
        }
    }
    $("#btnPrintSPB").click(function () {
        window.open("../../SP_COMMON/PrintPage/CKQXL_SPZRB.shtml?PROJECT_ID=" + m_project_id + "&TITLE=采矿权申请审批责任表");
    });

    objSel = $("#selOpinionModel_审查人意见");
    BindOpinionModelList(objSel);
    $(objSel).change(function () {
        if ($(this).val() == "") return;
        var vId = $(objSel).val();
        var url = DBCenterServiceUrl + "/WLMQ_OpinionModel/WLMQ_OpinionModel.asmx/GetOpinionModelContent";
        var param = "{'ID':" + vId + "}";
        GetSmJSON(url, param, function (str) {
            $("textarea[name='txt审查人意见']").val(str);
        });
    });
});

function BindOpinionModelList(obj) {
    var url = DBCenterServiceUrl + "/WLMQ_OpinionModel/WLMQ_OpinionModel.asmx/GetOpinionModelList";
    var param = "{'UserId':" + m_user.StaffID + ",'WorkflowID':0,'DepartmentID':0}";
    GetSmJSON(url, param, function (json) {
        callback(json, obj);
    });
}

function callback(json, obj) {
    var data = JSON.parse(json);
    if (data.Rows.length == 0) return;
    $("#" + obj.attr("id") + " option").each(function () {
        $(this).remove();
    });
    var vhtml = '<option value="0"></option>';
    for (var i = 0; i < data.Rows.length; i++) {
        vhtml += '<option value="' + data.Rows[i][0] + '">' + data.Rows[i][2] + '</option>';
    }
    $(obj).html(vhtml);
}

function InitSQB() {
    var obj = m_obj;
    for (i = 0; i < obj.Data.Data.length; i++) {
        var key = obj.Data.Data[i].Key;
        var value = obj.Data.Data[i].Value;
        if ($("input[name='txt" + key + "']") != null) {
            $("[name='txt" + key + "']").val(value);
        }
    }
    $("[name='txt经济类型']").val(metaconsts.GetValue(obj.GetValue("经济类型"), "KCK_JJLX"));
    $("[name='txtKCKZ']").val(obj.GetValue("开采主矿种") + "-" + metaconsts.GetValue(obj.GetValue("开采主矿种"), "KZ"));
}

function SQPH_Click() {
    $.ajax({
        url: DBCenterServiceUrl + "/KZ_SPDJ/SPCommon.asmx/WriteBPXML2",
        type: "POST",
        dataType: "json",
        data: "{'project_id':'" + m_project_id + "','fileName':'CKBG" + $("#txtksmc").val() + "'}",
        contentType: "application/json; charset=utf-8",
        success: function (json) {
            SaveAs();
        },
        error: function (x, e) {
        },
        complete: function (x) {
        }
    });
}
function SaveAs() {
    //    var m_SQPH = WFServiceUrl + "/temp/申请配号" + m_project_id + ".ZIP";
    var m_SQPH = DBCenterServiceUrl + "/KZ_SPDJ/SPCommon.asmx/Download?filename=CKBG" + $("#txtksmc").val() + ".xml"
    window.location.href = m_SQPH;
    
};

function JSPH() {
    var ss = document.getElementById("id_input_file");
    ss.click();

}
function getPath(obj) {
    if (obj) {
        if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
            obj.select();
            //obj.blur();
            return document.selection.createRange().text;
        }
        else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            if (obj.files) {
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
}
function selectedFileChanged() {
    var fileselector = document.getElementById("id_input_file");
    $("#id_input_selectedfile").val(fileselector.value);
    var path = getPath(fileselector);
    var appname, appcategory, apptype, ckguid, licenceid;
    if ($.browser.msie) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.load(path);
        appname = xmlDoc.getElementsByTagName("app_name")[0].childNodes[0].nodeValue;
        appcategory = xmlDoc.getElementsByTagName("app_category")[0].childNodes[0].nodeValue;
        apptype = xmlDoc.getElementsByTagName("app_type")[0].childNodes[0].nodeValue;
        ckguid = xmlDoc.getElementsByTagName("ck_guid")[0].childNodes[0].nodeValue;
        licenceid = xmlDoc.getElementsByTagName("licence_id")[0].childNodes[0].nodeValue;
        validate(appname, appcategory, apptype, ckguid, licenceid);
    } else if ($.browser.safari) {
        var xmlDom = document.implementation.createDocument("", "doc", null);
        var filereader = new FileReader();
        filereader.readAsText(fileselector.files[0]);
        filereader.onloadend = function () {
            var str = this.result;
            var jsonobj = $.xml2json(str);
            appname = jsonobj.app_name;
            appcategory = jsonobj.app_category;
            apptype = jsonobj.app_type;
            ckguid = jsonobj.ck_guid;
            licenceid = jsonobj.licence_id;
            validate(appname, appcategory, apptype, ckguid, licenceid);
        }
    }
    fileselector.value = '';

}
function validate(appname, appcategory, apptype, ckguid, licenceid) {
    if (appname != m_obj.GetValue("矿山名称")  || appcategory != "采矿权" || apptype != "采矿权变更") {
        alertex("导入的配号文件与当前项目不符");
        return;
    }
    $("#txtZHENGHAO").val(licenceid);
    m_obj.SetValue("XKZH", licenceid);
    m_obj.Update();
    alertex("读取配号成功", true);
    //$("#btnPrintXKZ").removeClass("nosee");
    //$("#btnPrintXKZH").removeClass("nosee");
}
function BLWC() {
    var condittion = "PROJECT_ID=\\\'" + m_project_id + "\\\'";
    m_obj.GetFirstSmObject(condittion);
    if (m_obj.GetValue("SFDZ") == "0") {
        alertex("入库失败，请先打印许可证");
        return;
    }
    var url = DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKQBG.asmx/FinishProject";
    var data = "{'project_id':'" + m_project_id + "'}";
    connectajax(url, function (txt) {
        $("#btnBLWC").css("display", "none");
        al
    }, data, true, null);
    MoverToLS(m_obj);
    var value = m_obj.GetValue("QYZB");
    if (value != null && value != "") {
        var points = GetPointsFromStr(value);
        points.length = points.length - 1;
        var fields = ["BSM", "XKZH", "开采主矿种", "矿山名称", "KZMC", "GXSJ"];
        var values = [m_obj.GetValue("BSM"), m_obj.GetValue("XKZH"), m_obj.GetValue("开采主矿种"), m_obj.GetValue("矿山名称"), GetKZMCbyKZDM(m_obj.GetValue("开采主矿种")), GetServerDate()];
        saveGeometry(points, savegeometryurl, fields, values);
    }
}
function succallback(json) {
    alertex(json);
}
function PrintXKZFB() {
    window.open("../../SP_COMMON/PrintPage/CKQXL_XKZFB.shtml?PROJECT_ID=" + m_project_id);
}
function PrintXKZ() {
    window.open("../../SP_COMMON/PrintPage/CKQXL_XKZ.shtml?PROJECT_ID=" + m_project_id);
}